﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PersonalWebSite.Common
{
    /// <summary>
    /// Defines the ClientReferencesManager class responsible for managing JS and CSS refs
    /// </summary>
    public class ClientReferencesManager
    {
        #region Variables
        List<string> cssFiles = new List<string>();
        List<string> jsFiles = new List<string>();
        #endregion

        #region Properties
        /// <summary>
        /// Gets the registered CSS files
        /// </summary>
        public string[] CssFiles
        {
            get
            {
                return cssFiles.ToArray();
            }
        }

        /// <summary>
        /// Gets the registered JS files
        /// </summary>
        public string[] JsFiles
        {
            get
            {
                return jsFiles.ToArray();
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Includes the given CSS reference
        /// </summary>
        /// <param name="path">CSS file path</param>
        public void IncludeCss(string path)
        {
            if (cssFiles.Contains(path))
                return;

            cssFiles.Add(path);
        }

        /// <summary>
        /// Includes the given CSS references
        /// </summary>
        /// <param name="paths">CSS file paths</param>
        public void IncludeCssRange(IEnumerable<string> paths)
        {
            cssFiles.AddRange(paths.Where(p => !cssFiles.Contains(p)).ToArray());
        }

        /// <summary>
        /// Includes the given JS reference
        /// </summary>
        /// <param name="path">JS file path</param>
        public void IncludeJs(string path)
        {
            if (jsFiles.Contains(path))
                return;

            jsFiles.Add(path);
        }

        /// <summary>
        /// Includes the given JS references
        /// </summary>
        /// <param name="paths">JS file paths</param>
        public void IncludeJsRange(IEnumerable<string> paths)
        {
            jsFiles.AddRange(paths.Where(p => !jsFiles.Contains(p)).ToArray());
        }
        #endregion

        #region Helper Classes
        /// <summary>
        /// Defines a client file info
        /// </summary>
        public class FileInfo
        {
            public string Path { get; set; }
            public string PhysicalPath { get; set; }
            public string Content { get; set; }
        }

        public class UrlClientReferences
        {
            public UrlClientReferences()
            {
                Url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
            }

            public string Url { get; set; }
            public IEnumerable<FileInfo> CssFiles { get; set; }
            public IEnumerable<FileInfo> JsFiles { get; set; }
            public string CssMinifiedAndCombinedContent { get; set; }
            public string JsMinifiedAndCombinedContent { get; set; }
        }
        #endregion
    }
}